$(function($){
	
	$("input[name^='uploadFile']").change(function(){
		uploadFile(this);
	});
	
	function uploadFile(_this){
		var $parent = $(_this).parents('div.ajaxUpImage');
		var $loading = $parent.find('img.loading');
		var $view = $parent.find('img.view');
		var $prefix = $parent.find('input.prefix');
		var $temp = $('#temp');
		var $sign = $('input.sign');
		var $type = $('input.type');
		var callback = $('#callback').val();
		var fileElementId = _this.name;
		var url = '/upload/image.do?fileElementId='+fileElementId+'&type='+$type.val()+'&prefix='+$prefix.val()+'&temp='+$temp.val()+'&sign='+$sign.val();
		$.ajaxFileUpload({
			url : url,
			secureuri : false,
			fileElementId : fileElementId,
			dataType : 'json',
			beforeSend : function(){
				$loading.show();
			},
			success : function(data,status){
				$loading.hide();
				$prefix.val(data.prefix);
				if(!$temp.val()){
					$temp.val(data.temp);
				}
				$view.attr('src',data.url+data.dir+"/"+data.prefix+'-t.jpg?'+new Date().getTime()).show();
				if(callback){
					window[callback]($('#'+fileElementId)[0],data.url+data.dir+"/"+data.prefix);
				}
			},
			error : function(data, status, e) {
				alert(status+ ':' + e + '\n' + data);
			}
		});
		$('#'+fileElementId).change(function(){
			uploadFile(this);
		});
	}
});
//显示上传图片
function showUpImage(o,id){
	isImg = function(filename){
		if(filename=='.gif'||filename=='.jpg'||filename=='.jpeg'||filename=='.png'||filename=='.bmp')
			return true;
		else
			return false;
	}
	imgRemove = function(){
		$('#uploadBox').remove();
	}
	var html = '<div id="uploadBox" class="uploadBox">';
		html += '<p>文件大小限制：小于1M</p>';
		html += '<p>文件格式限制：gif/jpg/jpeg/png/bmp</p>';
		html += '<label><input type="text" id="iptfile" class="textBox" readonly /><input type="file" size="1" id="uploadFile" name="uploadFile" hidefocus="true" /><span>浏览..</span><input type="reset" value="取消" class="closeBox" id="uploadRefuse" /><input type="button" class="iptButton" value="上传" id="uploadAccept" /></label></div>';
	if($('#uploadBox').size()>0) imgRemove();
	
	var obtn = $(o).offset();
	$(html).appendTo('body').css({left:obtn.left+60,top:obtn.top});
	
	$('#uploadFile').change(function(){
		$('#iptfile').val(this.value);
	});
	$('#uploadRefuse').click(function(){
		imgRemove();
	});
	$('#uploadAccept').click(function(){
		var filename = $("#uploadFile").val();
		if (filename != ''){
			filename = filename.substring(filename.lastIndexOf('.'));
			if(isImg(filename)){
				$.ajaxFileUpload({
					url : '/upload/image.do',
					secureuri : false,
					fileElementId : 'uploadFile',
					dataType : 'json',
					success : function(data, status) {
						if(data.result){
							$('#'+id).val(data.file);
							$('#showImageBox').html('<img src="'+data.file+'" />');
						}else{
							alert('文件上传失败！');
						}
						imgRemove();
					},
					error : function(data, status, e) {
						alert(status+ ':' + e + '\n' + data);
						imgRemove();
					}
				})
			}else{
				alert('请确认上传文件的格式！');
			}
		} else {
			alert('上传路径不可以为空！');
		}	
	});
}